/**
 * @Title: ProjectDaoJpa.java 
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">Psammead</a>
 * @date 2011-7-16 下午02:53:37 
 * @version 1.0
 * Copyright 2011 by Psammead
 */
package com.psammead.dms.project.dao.jpa;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.psammead.dms.common.core.bo.Page;
import com.psammead.dms.common.core.dao.jpa.GenericDaoJpa;
import com.psammead.dms.project.dao.ProjectDao;
import com.psammead.dms.project.entity.Project;

/**
 * @ClassName: ProjectDaoJpa
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">psammead</a>
 * @version 1.0
 * @date 2011-7-16
 */
@Repository("projectDao")
public class ProjectDaoJpa extends GenericDaoJpa<Project, Long> implements
		ProjectDao {
	/**
	 * 构造函数，继承父类的构造
	 * <p>Title: </p>
	 * <p>Description: </p>
	 */
	public ProjectDaoJpa(){
		super(Project.class);
	}
	
	/**
	 * 拼接查询条件
	 * @Title: sqlBuffer
	 * @Description: TODO
	 * @param paramMap
	 * @return StringBuffer
	 * @throws
	 */
	protected StringBuffer sqlMaker(Map<String, Object> paramMap){
		StringBuffer sb=new StringBuffer();
		if(!paramMap.keySet().isEmpty()){
			for(String key:paramMap.keySet()){
				if("id".equals(key)){
					sb.append(" and id =:id");
				}
				if("name".equals(key)){
					sb.append(" and name like :name");
					paramMap.put(key, "%"+(String)paramMap.get(key)+"%");
				}
				if("address".equals(key)){
					sb.append(" and address like :address");
					paramMap.put(key, "%"+(String)paramMap.get(key)+"%");
				}
				if("customer".equals(key)){
					sb.append(" and customer =:customer");
				}
				if("creator".equals(key)){
					sb.append(" and creator =:creator");
				}
				if("submiter".equals(key)){
					sb.append(" and submiter =:submiter");
				}
				if("updater".equals(key)){
					sb.append(" and updater =:updater");
				}
				if("charger".equals(key)){
					sb.append(" and charger =:charger");
				}
				if("status".equals(key)){
					sb.append(" and status =:status");
				}
				if("createTimeBefore".equals(key)){
					sb.append(" and createTime <:createTimeBefore");
				}
				if("createTimeAfter".equals(key)){
					sb.append(" and createTime >:createTimeAfter");
				}
				if("submitTimeBefore".equals(key)){
					sb.append(" and submitTime <:submitTimeBefore");
				}
				if("submitTimeAfter".equals(key)){
					sb.append(" and submitTime >:submitTimeAfter");
				}
				if("updateTimeBefore".equals(key)){
					sb.append(" and updateTime <:updateTimeBefore");
				}
				if("updateTimeAfter".equals(key)){
					sb.append(" and updateTime >:updateTimeAfter");
				}
				if("signTimeBefore".equals(key)){
					sb.append(" and signTime <:signTimeBefore");
				}
				if("signTimeAfter".equals(key)){
					sb.append(" and signTime >:signTimeAfter");
				}
				if("planStartTimeBefore".equals(key)){
					sb.append(" and planStartTime <:planStartTimeBefore");
				}
				if("planStartTimeAfter".equals(key)){
					sb.append(" and planStartTime >:planStartTimeAfter");
				}
				if("planEndTimeBefore".equals(key)){
					sb.append(" and planEndTime <:planEndTimeBefore");
				}
				if("planEndTimeAfter".equals(key)){
					sb.append(" and planEndTime >:planEndTimeAfter");
				}
				if("actStartTimeBefore".equals(key)){
					sb.append(" and actStartTime <:actStartTimeBefore");
				}
				if("actStartTimeAfter".equals(key)){
					sb.append(" and actStartTime >:actStartTimeAfter");
				}
				if("actEndTimeBefore".equals(key)){
					sb.append(" and actEndTime <:actEndTimeBefore");
				}
				if("actEndTimeAfter".equals(key)){
					sb.append(" and actEndTime >:actEndTimeAfter");
				}
			}
		}
		return sb;
	}

	@Override
	public List<Project> queryResultList(Map<String, Object> paramMap,
			LinkedHashMap<String, String> sortMap) {
		return this.queryResultList(new JsqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from Project o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap);
	}

	@Override
	public Page<Project> queryResultPage(Map<String, Object> paramMap,
			LinkedHashMap<String, String> sortMap, int pageNo, int pageSize) {
		return this.queryResultPage(new SqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from Project o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
			@Override
			public String buildCountSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select count(o.id) from Project o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap, pageNo, pageSize);
	}

}
